<?php

namespace Admin\Controller;

class MaterialController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Material");
    }

    //首页
    public function index() {
        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["m.enterprise_code"] = $user["enterprise_code"];
        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign('level0', $_REQUEST['p#name-like']);
        $this->assign('level1', $_REQUEST['c#name-like']);

        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }
    public function remove() {

        $id = $_REQUEST['id'];
        $this->currentModel->deleteById($id);

        $this->success('删除成功!', U("Material/index"));

    }

    public function add() {
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
//        $list0 = D('Code')->where(array('level' => 0, 'enterprise_code'=>$user["enterprise_code"]))->select();
//        $list1 = D('Code')->where(array('level' => 1, 'enterprise_code'=>$user["enterprise_code"]))->select();
        $levelCode = D('Code')->alias("c")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->join("left join __MATERIAL__ m on m.code_id = c.code_id")
            ->where(array('c.enterprise_code'=>$user["enterprise_code"]))
            ->field("c.*, c.name as code_name, p.name as parent_name, m.material_id, m.material_name")
            ->select();
        $this->assign("levelCode", $levelCode);
        $this->assign("user", $user);
//        $this->assign('list0', $list0);
//        $this->assign('list1', $list1);
        $this->display("add");
    }

    /**
     * 添加数据
     */
    public function addPost() {
        $data["code_id"] = $_REQUEST["codeId"];
        $data["material_name"] = $_REQUEST["name"];
        $data["spec"] = $_REQUEST["spec"];
        $data["prop"] = $_REQUEST["prop"];
        $data["unit"] = $_REQUEST["unit"];
        $data["barcode"] = $_REQUEST["barcode"];
        $data["remark"] = $_REQUEST["remark"];
        $data["create_time"] = getCurrentDate();
        $data["update_time"] = getCurrentDate();

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["update_id"] = $user["employee_id"];
        $data["enterprise_code"] = $user["enterprise_code"];
        $this->currentModel->add($data);

        $this->success("添加成功", U("Material/index"));

    }

    public function edit() {
        $id = $_REQUEST["id"];
        $info = D("Material")->queryById($id);
        $this->assign("info", $info);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $levelCode = D('Code')->alias("c")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->join("left join __MATERIAL__ m on m.code_id = c.code_id")
            ->where(array('c.enterprise_code'=>$user["enterprise_code"]))
            ->field("c.*, c.name as code_name, p.name as parent_name, m.material_id, m.material_name")
            ->select();
        $this->assign("levelCode", $levelCode);

        $code = D('Code')->where(array('code_id' => $info["code_id"]))->find();
        $parentCode = D('Code')->where(array('code_id' => $code["parentid"]))->find();
        $this->assign('codeName', $parentCode["name"].".".$code["name"]);
        $this->assign("user", $user);
        $this->display("edit");
    }
    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];
        $data = $this->currentModel->queryById($id);
//        $data["code_id"] = $_REQUEST["codeId"];
        $data["material_name"] = $_REQUEST["name"];
        $data["spec"] = $_REQUEST["spec"];
        $data["prop"] = $_REQUEST["prop"];
        $data["unit"] = $_REQUEST["unit"];
        $data["barcode"] = $_REQUEST["barcode"];
        $data["remark"] = $_REQUEST["remark"];
        $data["update_time"] = getCurrentDate();

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["update_id"] = $user["employee_id"];

        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("Material/index"));

    }
}